﻿using MbUnit.Framework;
using MobileBroadbandUserBlog.MBLM.Core.Interfaces;
using Rhino.Mocks;
using MobileBroadbandUserBlog.MBLM;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace MobileBroadbandLoggingMonitor.FastTests
{
    [TestFixture]
    public class MobileBroadbandLoggingMonitorTests
    {
        private readonly MockRepository _mockRepository = new MockRepository();
        private IMobileSoftwareFactory _mobileSoftwareFactory;
        private IMobileSoftware _mobileSoftware;

        [SetUp]
        public void SetUp()
        {
            _mobileSoftwareFactory = _mockRepository.StrictMock<IMobileSoftwareFactory>();
            _mobileSoftware = _mockRepository.StrictMock<IMobileSoftware>();
        }

        [Test]
       
        public void IsMobileSoftwarePresentOnSystemTest()
        {
            MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor monitor = new MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor();
        }

        [Test]
        [Row(true)]
        [Row(false)]
        public void IsMobileSoftwarePresentOnSystemTest(bool present)
        {
            var coll = new ReadOnlyCollection<IMobileSoftware>(new IMobileSoftware[] {_mobileSoftware});
            using (_mockRepository.Record())
            {
                if (present)
                {
                    Expect.Call(_mobileSoftware.IsPresent()).Return(true);
                    Expect.Call(_mobileSoftwareFactory.GetMobileBroadbandSoftware()).Return(coll);
                }
                else
                {
                    Expect.Call(_mobileSoftwareFactory.GetMobileBroadbandSoftware()).Return(new ReadOnlyCollection<IMobileSoftware>(new IMobileSoftware[] { }));
                }
            }
            using (_mockRepository.Playback())
            {
                MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor mobileBroadbandLoggingMonitor = new MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor(_mobileSoftwareFactory);
                bool gnu = mobileBroadbandLoggingMonitor.IsMobileSoftwarePresentOnSystem();
                Assert.AreEqual(present, gnu);
            }
        }


        [Test]
        [Row(true)]
        [Row(false)]
        public void GetMobileSoftwareLoggerTest(bool present)
        {
            var coll = new ReadOnlyCollection<IMobileSoftware>(new IMobileSoftware[] { _mobileSoftware });
            using (_mockRepository.Record())
            {
                if (present)
                {
                    Expect.Call(_mobileSoftware.IsPresent()).Return(true).Repeat.Twice();
                    Expect.Call(_mobileSoftwareFactory.GetMobileBroadbandSoftware()).Return(coll).Repeat.Twice();
                }
                else
                {
                    Expect.Call(_mobileSoftwareFactory.GetMobileBroadbandSoftware()).Return(new ReadOnlyCollection<IMobileSoftware>(new IMobileSoftware[] { }));
                }
            }
            using (_mockRepository.Playback())
            {
                MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor mobileBroadbandLoggingMonitor = new MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor(_mobileSoftwareFactory);
                IMobileSoftwareLogger logger = mobileBroadbandLoggingMonitor.GetMobileSoftwareLogger();
                if (present)
                {
                    Assert.AreEqual(_mobileSoftware, logger.GetMobileSoftware());
                }
                else
                {
                    Assert.IsNull(logger);
                }
            }
        }

        [Test]
        public void GetMobileSoftwareLoggerUserDefinedLocationTest()
        {
            string path = "the path";

            MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor mobileBroadbandLoggingMonitor = new MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor(_mobileSoftwareFactory);
            IMobileSoftwareLogger logger = mobileBroadbandLoggingMonitor.GetMobileSoftwareLogger(path);
            Assert.AreEqual(path, logger.GetMobileSoftware().GetFolderToMonitor());
        }

        [Test]
        public void GetMobileSoftwareLoggerSelectedExternallyTest()
        {
            MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor mobileBroadbandLoggingMonitor = new MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor(_mobileSoftwareFactory);
            IMobileSoftwareLogger logger = mobileBroadbandLoggingMonitor.GetMobileSoftwareLogger(_mobileSoftware);
            Assert.AreEqual(_mobileSoftware, logger.GetMobileSoftware());
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void NullTestConstructor()
        {
            MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor mobileBroadbandLoggingMonitor = new MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor(null);
        }
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void NullGetMobileSoftwareLoggerFromInterface()
        {
            MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor mobileBroadbandLoggingMonitor = new MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor(_mobileSoftwareFactory);
            mobileBroadbandLoggingMonitor.GetMobileSoftwareLogger(null as IMobileSoftware);
        }
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void NullGetMobileSoftwareLoggerFromPath()
        {
            MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor mobileBroadbandLoggingMonitor = new MobileBroadbandUserBlog.MBLM.MobileBroadbandLoggingMonitor(_mobileSoftwareFactory);
            mobileBroadbandLoggingMonitor.GetMobileSoftwareLogger(null as string);
        }

    }
}